
import { ccs } from './services';
import { WinBase } from '../wins/WinBase';
import { winConfig } from './winConfig';
const { ccclass, property } = cc._decorator;

@ccclass
export class UICtrl extends cc.Component {
    @property(cc.Prefab)
    private maskPrefab:cc.Prefab = null;
    @property(cc.Prefab)
    private baseRoot:cc.Prefab = null;
    @property(cc.Prefab)
    private touchMask:cc.Prefab = null;

    private _prefabs = {};
    private _touchMaskNode:cc.Node = null;          // 屏蔽触摸节点

    start () {
        ccs.winMgr.setWinCtrl(this);
    }


    // 显示界面
    showWin(winName:string, args?:object, func?:Function, target?:object) {
        if(!this._prefabs || !this.node.isValid) {
            return ;
        }
        
        this.newWin(winName, args, func, target);
    }
    
    // 添加新界面
    private newWin(winName:string, args?:object, func?:Function, target?:object) {
        this.showTouchMask();
        let targs = (args || {}) as any;
        let winType = targs.winType||3;
        let data = winConfig.getCfg(winName);
        let root = cc.instantiate(this.baseRoot);
        if(root) {
            this.node.addChild(root, targs.zOrder || 3, winName);
            // 是否需要黑色遮罩
            let winCfg = winConfig.WIN_TYPE_CFG[data.type];
            if(winCfg[2]) {
                let maskNode = cc.instantiate(this.maskPrefab);
                if(targs.mask || data.mask) {
                    maskNode.opacity = 255 * (targs.mask || data.mask);
                }
                if(maskNode && winCfg[1]) {
                    maskNode.on(cc.Node.EventType.TOUCH_START, (target, i) => {
                        let winScript = target.target.parent.getComponent(winName);
                        if(winScript) {
                            winScript._close();
                        }
                    })
                   
                }
                
                root.addChild(maskNode);
            }
             //窗口预制
            let prefab = this._prefabs[winName];
            let addWin = ()=>{
                if(!root.isValid) {
                    return ;
                }
                let winNode = cc.instantiate(prefab);
                if(winNode) {
                    root.addChild(winNode);
                    let winScript = winNode.getComponent(WinBase);
                    //关闭窗口
                   
                    if(winScript) {
                        winScript._initArgs(args, winType);
                        ccs.gf.doCallback(func, target, winScript);
                    }
                }
                // this.hideTouchMask();
            }

            if(prefab) {
                addWin();
            } else {
                ccs.resMgr.loadRes("prefab/wins/" + winName, cc.Prefab).then((winPrefab)=>{
                    if(!this.isValid) {
                        return ;
                    }
                    prefab = winPrefab;
                    this._prefabs[winName] = winPrefab;
                    addWin();
                })
            }
        }
    }
    // 获取触摸屏蔽预制
    getTouchMask():cc.Prefab {
        return this.touchMask;
    }
    // 添加触摸屏蔽
    showTouchMask() {
        if(!this._touchMaskNode) {
            this._touchMaskNode = cc.instantiate(this.touchMask);
            this.node.addChild(this._touchMaskNode);
        }
        this._touchMaskNode.active = true;
    }

    // 隐藏屏蔽触摸
    hideTouchMask() {
        if(this._touchMaskNode)
            this._touchMaskNode.active = false;
    }
}

